<?php

namespace app\admin\controller;

use think\captcha\Captcha;
use think\Controller;
use think\Request;
use app\admin\validate\LoginValidate;
use app\admin\model\Admin;



class Login extends Controller
{
   
    public function index()
    {
        return $this->fetch();
    }

    public function verify()
    {
        $config =    [
            // 验证码字体大小
            'fontSize'    =>    20,    
            // 验证码位数
            'length'      =>    3   ,   
            // 关闭验证码杂点
            'useNoise'    =>false, 
            'useCurve'  => false
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }
    
    public function save()
    {
        $data=request()->param();    
        $validate=new LoginValidate();
        if (!$validate->check($data)) {         
            return ['code'=>200,'msg' => $validate->getError()];
        }   
        if(!captcha_check($data['code'])){
            return ['code'=>400,'msg' =>'验证码错误'];
        }

        $db=new Admin();
        $res=$db->login($data);
        if ($res){
            return ['code'=>200,'msg' =>'登录成功'];
        }else {
            return ['code'=>400,'msg' =>'账号或密码错误'];
        }
        
    }
    public function logout()
    {
        session('aid',null);
        session('aname',null);

        return ['code'=>400,'msg'=>'退出成功'];
    }
}
